package cn.ctubu.edu.dp02.service;

import cn.ctubu.edu.dp02.dao.TeacherRepository;
import cn.ctubu.edu.dp02.etity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    public TeacherRepository teacherRepository;

    /**
     *
     * @return ListTeacher
     */
    public List<Teacher> findAll(){

        return teacherRepository.findAll();
    }

    /**
     *按id 进行查询
     * @param id 主键，整数
     * @return
     */
    public Teacher findById(Integer id){


        return teacherRepository.findById(id).orElse(null);
    }

    /**
     * 按名字查询，Like
     * @param name
     * @return
     */
  public List<Teacher> findByName(String name){
      return teacherRepository.findByNameLike(name);

  }
    /**
     * 按名字和密码查询
     * @param name and password
     * @return
     */
    List<Teacher> findByNameAndPassword(String name, String password){
        return teacherRepository.findByNameAndPassword(name,password);
    }

    /**
     * 插入
      * @param teacher
     */
    public void insert(Teacher teacher){
        teacherRepository.save(teacher);
    }

    /**
     * add
     * @param teacher
     * @return
     */
    public Teacher add(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * update
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    public void delete(Integer id){
    teacherRepository.deleteById(id);
    }
}

